package cn.hotel.servlet;

import cn.hotel.pojo.Member;
import cn.hotel.service.MemberService;
import cn.hotel.service.MemberServiceImpl;
import cn.hotel.utlis.MapUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
@WebServlet(value = {"/member"})
public class MemberLoginServlet extends HttpServlet {
      private MemberService memberService = new MemberServiceImpl();

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行处理GET请求的方法.................");
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行处理POST请求的方法.................");

        //创建session对象
        HttpSession session = req.getSession();
        //获取系统根路径
        String appPath = req.getContextPath();
        //设置响应头
        resp.setContentType("text/html;charset=UTF-8");
        //创建out对象
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String account = req.getParameter("account");
        String password = req.getParameter("password");
        Map<String,Object> resultMap = memberService.login(account,password);
        String flag = MapUtil.getString(resultMap,"flag");
        if("1".equals(flag)){
            //登录成功
            Member member = (Member) resultMap.get("data");
            session.setAttribute("login",member);
            out.print("<script>alert('登录成功！');location.href='"+appPath+"/memberwork?type=list';</script>");
        }else {
            out.print("<script>alert('登录失败！');location.href='"+appPath+"/memberjsp/login.jsp';</script>");
        }
        //清空out流
        out.flush();
    }



}
